home *** CD-ROM | disk | FTP | other *** search
- int state,iptr,nchar,n_in_buf, need_cpress;
- char g_r_buf[320], g_cmp[360], *p_r_buf = &g_r_buf[0], *p_cmp = &g_cmp[0];
- char *beg_ras = "\x1b*r#A";
- char *ras_cmd = "\x1b*b";
- char *end_ras = "\x1b*rB";
- char *myitoa(int, char *);
- void flush();
- void flush2();
- void send (char *);
- void pchar(unsigned char);
-
- c_init() {
- need_cpress = state = iptr = 0;
- }
- c_filt(int c) {
- int unique, lenloc,in_unique;
- int g, run, runmax = 127, put;
- int i;
- int n,p;
- char *r_buf, *cmp;
- r_buf = p_r_buf;
- cmp = p_cmp;
- switch (state) {
- case 0: {
- if ( c == beg_ras[iptr]) {
- iptr++;
- if (iptr == 3) state = 1;
- else if (iptr == 5) {
- /* have a complete start raster cmd */
- state = 2;
- flush(); /* send out command */
- need_cpress = 1; /* postpone compress cmd */
- }
- }
- else {
- if (iptr != 0) flush();
- pchar(c);
- }
- break;
- }
- case 1: { /* looking for digit 0 or 1 in start raster */
- if (c == '0' || c == '1') {
- beg_ras[iptr] = c;
- iptr++;
- state = 0;
- }
- else if (c == 'A') {
- state = 2;
- flush();
- pchar(c);
- need_cpress = 1; /* postpone compress cmd */
- }
- else {
- flush();
- pchar(c);
- state = 0;
- }
- break;
- }
- case 2: { /* looking for transfer raster graphics cmd */
- /* search for <esc>*bnnnW */
- if ( c == ras_cmd[iptr]) {
- iptr++;
- if (iptr == 3) {
- state = 3;
- nchar = 0;
- }
- }
- else {
- pchar(c);
- if (iptr == 2 && c == 'r') { /* is it a <esc>*rB ? */
- state = 6;
- }
- if (iptr != 0) flush2();
- }
- break;
- }
- case 3: { /* munching digits in raster command, looking for W */
- if (c >='0' && c <= '9') {
- nchar = nchar*10 + (c-'0');
- }
- else if (c == 'M' || c == 'm')
- { /* oh oh - got a compress code */
- /* abandon compression - it may already be compressed */
- send("\x1b*b");
- send(myitoa(nchar,r_buf));
- /* don't forget the 'M'!!!! */
- pchar('M');
- state = 12;
- iptr = 0;
- }
- else if (c != 'W') {
- if (iptr != 0) flush();
- pchar(c);
- }
- else { /* we have a complete command header */
- if (nchar != 0) {
- state = 4;
- n_in_buf = 0;
- if (need_cpress) {
- send("\x1b*b2M");
- need_cpress = 0;
- }
- send("\x1b*b");
- }
- else {
- send("\x1b*bW");
- state = 2;
- iptr = 0;
- }
- }
- break;
- }
- case 4: { /* putting a run in the buffer */
- r_buf[n_in_buf++] = c;
- if (n_in_buf == nchar) {
- unique = lenloc = in_unique = put = 0;
- for (g=0;g<nchar;) {
- for (run=1;(r_buf[g]==r_buf[g+run])
- && ( (g+run) < nchar)
- && ( run < runmax); run++ );
- if (run > 2) {
- if (in_unique) {
- cmp[lenloc] = unique - 1;
- }
- cmp[put++] = -(run-1);
- cmp[put++] = r_buf[g];
- in_unique = 0;
- g += run;
- }
- else {
- if (unique >= runmax) {
- cmp[lenloc] = unique - 1;
- in_unique = 0;
- }
- if (!in_unique) {
- lenloc = put;
- cmp[put++] = 0; /* placeholder */
- in_unique = 1;
- unique = 0;
- }
- cmp[put++] = r_buf[g];
- g++;
- unique++;
- }
- }
- if (in_unique) { /* fix up any leftover unique */
- cmp[lenloc] = unique + 1;
- }
- /* drop out the value of put */
- send(myitoa(put,r_buf));
- /* don't forget the 'W'!!!! */
- pchar('W');
- /* now put out 'put' bytes from cmp */
- for (g=0;g<put;g++) pchar(cmp[g]);
- state = 2;
- iptr = 0;
- }
- break;
- }
- case 12: { /* dealing with compressed data, ignore until <esc>*rB */
- /* search for <esc>*bnnnW */
- pchar(c);
- if ( c == ras_cmd[iptr]) {
- iptr++;
- if (iptr == 3) {
- state = 13;
- nchar = 0;
- }
- }
- else {
- if (iptr == 2 && c == 'r') { /* is it a <esc>*rB ? */
- state = 16;
- }
- }
- break;
- }
- case 13: { /* munching digits in raster command, looking for W */
- pchar(c);
- if (c >='0' && c <= '9') {
- nchar = nchar*10 + (c-'0');
- }
- else if (c != 'W') { /* some other strange command ? */
- state = 12;
- iptr = 0;
- }
- else { /* 'W' => we have a complete command header */
- if (nchar != 0) {
- state = 14;
- }
- else {
- state = 12;
- iptr = 0;
- }
- }
- break;
- }
- case 14: { /* putting a run in the buffer */
- pchar(c);
- if (n_in_buf == nchar) {
- state = 12;
- iptr = 0;
- }
- break;
- }
- case 6: { /* have received "<esc>*r", is this char a B? */
- pchar(c);
- iptr = 0;
- if ( c == 'B')
- {
- state = 0;
- }
- else {
- state = 2;
- }
- break;
- }
- case 16: { /* have received "<esc>*r", is this char a B? */
- pchar(c);
- iptr = 0;
- if ( c == 'B')
- {
- state = 0;
- }
- else {
- state = 12;
- }
- break;
- }
- }
- }
- void flush() {
- int i;
- for (i=0; i<iptr; i++) pchar(beg_ras[i]);
- iptr = 0;
- }
- void flush2() {
- int i;
- for (i=0; i<iptr; i++) pchar(ras_cmd[i]);
- iptr = 0;
- }
- void send(char *s) {
- char *c;
- c = s;
- while (*c) pchar(*c++);
- }
- char *myitoa(int num, char *str) {
- int n;
- int p = 5;
- n = num;
- str[p--] = '\0';
- while (n != 0) {
- str[p--] = (n % 10) + '0';
- n /= 10;
- }
- if (num == 0) str[p--] = '0';
- p++;
- do {
- str[n++] = str[p++];
- } while (str[p-1]);
- return (str);
- }
-